package com.beerandjesus.sportofkings.handicapper.dao;

import com.beerandjesus.sportofkings.handicapper.model.Sire;
import org.apache.commons.lang.StringUtils;

import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * Date: 6/4/13
 * Time: 6:56 PM
 */
public abstract class SireDAOImpl extends AbstractDAOImpl {
    private BaseSireDataFactory factory = new BaseSireDataFactory();
    private String[] baseDataFiles;

    protected void buildDatabase(String tableName) throws DAOException {
        Connection connection = getConnectionManager().getTurfSireDatabaseConnection();
        if (getBaseDataFiles() == null || getBaseDataFiles().length == 0) {
            throw new DAOException("No base data files provided");
        }
        executeStatementIgnoringException(connection, DROP_TABLE + tableName);
        executeStatement(connection, CREATE_TABLE + tableName +
                SireDAO.SIRE_TABLE_DEFINITION);
        PreparedStatement insert;
        try {
            List<BaseSireData> sires = factory.getBaseSireData(getBaseDataFiles());
            for (BaseSireData d : sires) {
                BigDecimal percentage = factory.getWinPercentage(d);
                insert = connection.prepareStatement("insert into " + tableName +
                        " (name, percentage) values (?, ?)");
                try {
                    insert.setString(1, d.getName());
                    insert.setBigDecimal(2, percentage);
                    insert.execute();
                } finally {
                    insert.close();
                }
            }
        } catch (IOException e) {
            throw new DAOException("Error reading base data file", e);
        } catch (SQLException e) {
            throw new DAOException("Error inserting data", e);
        }
    }

    protected int getRowCount(String tableName) throws DAOException {
        Connection connection = getConnectionManager().getTurfSireDatabaseConnection();
        return getRowCount(connection, tableName);
    }

    public String[] getBaseDataFiles() {
        return baseDataFiles;
    }

    public void setBaseDataFiles(String[] baseDataFiles) {
        this.baseDataFiles = baseDataFiles;
    }

    protected Sire getSireByName(String name, String tableName, Connection connection) throws DAOException {
        if (StringUtils.isEmpty(name)) {
            return null;
        }
        PreparedStatement smt = null;
        try {
            smt = connection.prepareStatement("select * from " + tableName + " where name = ?");
            smt.setString(1, name.toUpperCase());
            ResultSet rs = smt.executeQuery();
            try {
                if (rs == null || !rs.next()) {
                    return null;
                }
                String h = rs.getString("name");
                BigDecimal p = rs.getBigDecimal("percentage");
                return newSire(h, p);
            } finally {
                if (rs != null) {
                    try {
                        rs.close();
                    } catch (SQLException e) {
                        // ignore
                    }
                }
            }
        } catch (SQLException e) {
            throw new DAOException("Error fetching sire by name " + name, e);
        } finally {
            if (smt != null) {
                try {
                    smt.close();
                } catch (SQLException e) {
                    // ignore
                }
            }
        }
    }

    protected abstract Sire newSire(String name, BigDecimal winPercentage);
}
